package com.devunion.salon.server.rpc;

import com.devunion.salon.client.app.bean.DepartmentModel;
import com.devunion.salon.client.rpc.DepartmentService;
import com.devunion.salon.server.jpa.utils.ModelQueryHelper;
import com.devunion.salon.core.model.InventoryDepartment;
import com.extjs.gxt.ui.client.data.BaseListLoadResult;
import com.extjs.gxt.ui.client.data.ListLoadConfig;
import com.extjs.gxt.ui.client.data.ListLoadResult;
import com.extjs.gxt.ui.client.data.ModelData;
import org.gwtwidgets.server.spring.GWTSpringController;
import org.springframework.orm.jpa.JpaTemplate;

import java.util.List;
import java.util.ArrayList;

/**
 * @author spetrakovsky
 */
public class DepartmentServiceImpl extends GWTSpringController implements DepartmentService {

    private JpaTemplate jpaTemplate;

    public ListLoadResult<DepartmentModel> getDepartments(ListLoadConfig config) {
        List<InventoryDepartment> objects = jpaTemplate.find("select dep from InventoryDepartment dep");
        List<DepartmentModel> result = new ArrayList<DepartmentModel>(objects.size());
        for (InventoryDepartment object : objects) {
            result.add(new DepartmentModel(object.getId(), object.getName()));
        }
        return new BaseListLoadResult<DepartmentModel>(result);
    }


    public JpaTemplate getJpaTemplate() {
        return jpaTemplate;
    }

    public void setJpaTemplate(JpaTemplate jpaTemplate) {
        this.jpaTemplate = jpaTemplate;
    }
}
